<?php
defined( '_AppBackMiage' ) or die( 'Restricted access' );
if(isAdministrateur ()){
    include_once (DS."backoffice/include/header.php");
    include_once( DS."backoffice/include/toppanel_utilisateurs.php");
    ?>

    <div id="wrapper">
        <div id="content">
            <div id="box">
                <h3 id="adduser">Import massif par csv</h3>
                <?php
                if(isset($_POST['submit']) && !empty($_POST['submit']))
                {
                      
                    // import du fichier
                    $document = new Document();
                    $a = $document->upload($_FILES['csv'],DS."upload/import/");
                    if(isset($a) && strpos("Erreur",$a) === false)
                    {
                       
                        $fp = fopen($a['msg'],'r');
                        $i = 0;
                        $lstPromo = Promo::getListPromo();
                        $goodInsert=0;
                        while($line=fgets($fp))
                        {
                            $i++;
                            $l = trim($line);
                            if($i>=1 && !empty($l))
                            {
                                $u = explode(';',$l);
                                if(!Utilisateur::checkIfExistLogin($u[2]))
                                {
                                    $etudiant = new Etudiant();

                                    $etudiant->setNomUtilisateur($u[0]);
                                    $etudiant->setPrenomUtilisateur($u[1]);
                                    $etudiant->setEmailUtilisateur($u[2]);
                                    $promo = array_search(trim($u[3]),$lstPromo);

                                    $etudiant->setIdPromo($promo);
                                    $etudiant->setPassword($u[4]);
                                    $etudiant->insert();
                                    if($etudiant->requeteOk)
                                    {
                                        $goodInsert++;
                                    }
                                    unset ($etudiant);
                                }
                            }
                        }
                        fclose($fp);
                        unlink($a['msg']);
                        if($i == $goodInsert)
                        {
                            echo "Tout a bien ete insere";
                        }
                    }
                }
                else
                {
                    ?>
                    <form id="form" method="post" enctype="multipart/form-data">
                        <fieldset id="import">
                            <legend>Import du fichier</legend>
                            Fichier :  <input type="file" name="csv"/>
                            <br />
                        </fieldset>
                        <div align="center">
                            <input id="button1" type="submit" name="submit" value="Importer" />
                            <input id="button2" type="reset" />
                        </div>
                    </form>
                    <?php
                }
                ?>
            </div>
        </div>

    <?php
    include_once (DS."backoffice/include/sidebar.php");
    include_once (DS."backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>